package com.legal.utils.tools;

import com.legal.domain.po.practice_view.PracticeRecordsListView;
import com.legal.domain.po.practice_view.RecordDetailsView;
import com.legal.service.practice.impl.view.IPracticeRecordsListViewService;
import com.legal.service.practice.impl.view.IRecordDetailsViewService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@RequiredArgsConstructor
@Component
@Slf4j
public class RecordTool {
    private final IPracticeRecordsListViewService practiceRecordsListViewService;

    private final IRecordDetailsViewService recordDetailsViewService;
    @Tool(description = "当用户需要你帮助整理错题时，就调用本工具获取详细的刷题数据。你需要根据返回的刷题记录信息,总结一下用户错的题，并帮助分析用户常见的错误，并给出对应的建议")
    public List<List<RecordDetailsView>>  getAllRecordDetails(
            @ToolParam(description = "用户编号", required = true) String userNo,
            @ToolParam(description = "日期", required = false) LocalDateTime time){
        List<PracticeRecordsListView> list = practiceRecordsListViewService.getHistoryListByUserNo(userNo);
        // 通过时间过滤，比如time为6月27日，则返回2025年6月27日的所有记录（大于6月26，小于6月27）
        if (time != null){
            list = list.stream().filter(p->p.getStartTime().isAfter(time.minusDays(1)) && p.getEndTime().isBefore(time.plusDays(1))).toList();
        }
        List<List<RecordDetailsView>> allRecordDetails = list.stream().map(p->getRecordDetails(p.getRecordNo())).toList();
        return allRecordDetails;
    }

    public List<RecordDetailsView> getRecordDetails(@ToolParam(description = "刷题记录编号", required = true) String recordNo){
       return recordDetailsViewService.getPracticeRecordDetailsByRecordNo(recordNo);
    }
}
